package com.springWebFlux.handler;

import com.springWebFlux.Model.ResultViewModel;
import com.tony.akka.sample1.App;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.function.Consumer;
import java.util.function.Function;

import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
import static reactor.core.publisher.Flux.fromIterable;

/**
 * @version V1.0  </br>
 * @ClassName ResultHandler </br>
 * @Description TODO  </br>
 * @Author tonywang</br>
 * @DATE 3/20/2019 4:29 PM</br>
 */
public class ResultHandler {
    private ResultService mResultService = new ResultService();

    /**
     * get the result from service first, then trans the result to {@code ServerResponse}
     * @param serverRequest the req
     * @return the ServerResponse
     */
    public Mono<ServerResponse> extraResult(ServerRequest serverRequest) {
        //get the result from service
        //todo : do some check here.
        Mono<ResultViewModel> resultViewModelMono = mResultService.extraResult(serverRequest);
        Mono<ServerResponse> notFound = ServerResponse.notFound().build();
        //trans to ServerResponse and return.
        //todo : too many code

        return resultViewModelMono.flatMap(new Function<ResultViewModel, Mono<ServerResponse>>() {
            @Override
            public Mono<ServerResponse> apply(ResultViewModel resultViewModel) {
                return ServerResponse
                        .ok()
                        .contentType(APPLICATION_JSON)
                        .body(fromObject(resultViewModel));
            }
        }).switchIfEmpty(notFound);
    }

    public Flux<ServerResponse> listResult(ServerRequest pServerRequest) {
        Flux<ResultViewModel> allResult = mResultService.flowAllResult(pServerRequest);
        Mono<ServerResponse> notFound = ServerResponse.notFound().build();
        return allResult.flatMap(new Function<ResultViewModel, Mono<ServerResponse>>() {
            @Override
            public Mono<ServerResponse> apply(ResultViewModel resultViewModel) {
                return ServerResponse
                        .ok()
                        .contentType(APPLICATION_JSON)
                        .body(fromObject(resultViewModel));
            }
        }).switchIfEmpty(notFound);
    }

    public Mono<ServerResponse> createView(ServerRequest pServerRequest) {
        Mono<ResultViewModel> item = mResultService.putItem(pServerRequest);
        Mono<ServerResponse> notFound = ServerResponse.notFound().build();
        return item.flatMap(new Function<ResultViewModel, Mono<ServerResponse>>() {
            @Override
            public Mono<ServerResponse> apply(ResultViewModel resultViewModel) {
                return ServerResponse
                        .ok()
                        .contentType(APPLICATION_JSON)
                        .body(fromObject(resultViewModel));
            }
        }).switchIfEmpty(notFound);
    }
}
